import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/13 15:17
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.characterReplacement("AABABBA", 1);
    }
    public int characterReplacement(String s, int k) {
        int n = s.length();
        int left = 0, right = 0, max = 0;
        int[] cnt = new int[26];
        while (right < n) {
            int temp = ++cnt[s.charAt(right) - 'A'];
            max = Math.max(max, temp);
            if (right - left + 1 - max > k) {
                cnt[s.charAt(left) -'A']--;
                left++;
            }
            right++;
        }
        return right - left;
    }
}
